package com.surelution.projects.crm

class MemberCardController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [memberCardInstanceList: MemberCard.list(params), memberCardInstanceTotal: MemberCard.count()]
    }

    def create = {
        def memberCardInstance = new MemberCard()
        memberCardInstance.properties = params
        return [memberCardInstance: memberCardInstance]
    }
	
	def search = {
		def market = Market.get(params.id)
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		def memberCardList = MemberCard.createCriteria().list() {
			if(market){
				eq('market', market)
			}
			order("connectTime", "desc")
		}
		[memberCardList: memberCardList, memberCardListTotal:0,market:market]
	}
	
	def springSecurityService
    def save = {
        def memberCardInstance = new MemberCard(params)
		if (params.cardNo.size()==16){
			memberCardInstance.connectTime = new Date()
			memberCardInstance.holderName = Market.get(params.market.id).manager
			memberCardInstance.isMaster = params.isMaster?params.isMaster:0
			memberCardInstance.accountAcceptBranch = Market.get(params.market.id).station.name
	        if (memberCardInstance.save(flush: true)) {
				Market market = memberCardInstance.market
				if(market.marketType.id!=2){
					market.preMarketType = market.marketType
					market.marketType = MarketSpecies.get(2)
				}
				if(params.isMaster){
					market.marketCardNo = params.cardNo
				}
				market.save()
	            flash.message = "${message(code: 'default.created.message', args: [message(code: 'memberCard.label', default: 'MemberCard'), memberCardInstance.id])}"
	            redirect(action: "show", id: params.market.id, controller:"market")
	        }
	        else {
				flash.message = "${message(code: 'default.not.create.message')}"
	            redirect(action: "show", id: params.market.id, controller:"market")
	        }
		}
		else {
			flash.message = "${message(code: 'default.errorCard.message')}"
			redirect(action: "show", id: params.market.id, controller:"market")
		}
    }

    def show = {
        def memberCardInstance = MemberCard.get(params.id)
        if (!memberCardInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'memberCard.label', default: 'MemberCard'), params.id])}"
            redirect(action: "list")
        }
        else {
            [memberCardInstance: memberCardInstance]
        }
    }

    def edit = {
        def memberCardInstance = MemberCard.get(params.id)
        if (!memberCardInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'memberCard.label', default: 'MemberCard'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [memberCardInstance: memberCardInstance]
        }
    }

    def update = {
        def memberCardInstance = MemberCard.get(params.id)
        if (memberCardInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (memberCardInstance.version > version) {
                    memberCardInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'memberCard.label', default: 'MemberCard')] as Object[], "Another user has updated this MemberCard while you were editing")
                    render(view: "edit", model: [memberCardInstance: memberCardInstance])
                    return
                }
            }
            memberCardInstance.properties = params
            if (!memberCardInstance.hasErrors() && memberCardInstance.save(flush: true)) {
				if(!memberCardInstance.isMaster){
					Market m = Market.findByMarketCardNo(memberCardInstance.cardNo)
					if (m){
						m.marketCardNo = 0
						m.save()
					}
				}
				else{
					Market m = memberCardInstance.market
					m.marketCardNo = memberCardInstance.cardNo
					m.save()
				}
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'memberCard.label', default: 'MemberCard'), memberCardInstance.id])}"
                redirect(action: "show", id: memberCardInstance.id)
            }
            else {
                render(view: "edit", model: [memberCardInstance: memberCardInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'memberCard.label', default: 'MemberCard'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def memberCardInstance = MemberCard.get(params.id)
        if (memberCardInstance) {
            try {
                memberCardInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'memberCard.label', default: 'MemberCard'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'memberCard.label', default: 'MemberCard'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'memberCard.label', default: 'MemberCard'), params.id])}"
            redirect(action: "list")
        }
    }
}
